package cn.edu.szu.bigdata.secure;


import cn.edu.szu.bigdata.util.ConfigUtil;
import com.qihoo.wzws.rzb.cc.CC;
import com.qihoo.wzws.rzb.cc.CCAlgorithm;
import com.qihoo.wzws.rzb.cc.CCDetail;
import com.qihoo.wzws.rzb.secure.po.CCEntity;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;

public class CCAnalyzeSingle {
    private static int concurrent_request = 1000;
    private static double request_growth = 0.5;
    private static double ip_rate = 0.5;

    public CCAnalyzeSingle() {
    }

    public static void initParas() {
        concurrent_request = Integer.valueOf(ConfigUtil.formatConfig.get("cc_concurrent_request"));
        request_growth = Double.valueOf(ConfigUtil.formatConfig.get("cc_request_growth"));
        ip_rate = Double.valueOf(ConfigUtil.formatConfig.get("cc_ip_rate"));
    }

    public void execute(HashMap<String, TreeMap<Long, CCDetail>> allHost) {
        if (allHost != null && allHost.size() != 0) {
            Iterator i$ = allHost.entrySet().iterator();

            label50:
            while(i$.hasNext()) {
                Map.Entry hostEntry = (Map.Entry)i$.next();
                TreeMap<Integer, CCDetail> hMap = (TreeMap)hostEntry.getValue();
                Iterator iterator = hMap.entrySet().iterator();
                Map.Entry previous = null;
                if (iterator.hasNext()) {
                    previous = (Map.Entry)iterator.next();
                }

                while(true) {
                    Map.Entry current;
                    CCDetail previous5Min;
                    CCDetail current5Min;
                    SimpleDateFormat dateFormat;
                    do {
                        if (!iterator.hasNext()) {
                            continue label50;
                        }

                        current = (Map.Entry)iterator.next();
                        previous5Min = (CCDetail)previous.getValue();
                        current5Min = (CCDetail)current.getValue();
                        dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    } while(current5Min.getCount() < concurrent_request);

                    Date startime = null;
                    CC c = CCAlgorithm.compareAndGetResult(previous5Min, current5Min, request_growth, ip_rate);
                    if (c != null) {
                        CCEntity entity = new CCEntity(c.getHost(), c.getIp(), c.geturi().replaceAll("&", "&amp;").replaceAll(">", "&gt;").replaceAll("<", "&lt;").replaceAll("\"", "&quot;"), c.getTime(), c.getCount());
                        AttackReportCache.put2CCList(entity);
                        if (AnalyzeSingle.useRoutine) {
                            String[] day = c.getTime().split(" ");
                            String[] hour = c.getTime().split(":");
                            AttackReportCache.put2dayCCAttackMap(day[0], c.getCount());
                            AttackReportCache.put2hourCCAttackMap(hour[0], c.getCount());
                            AttackReportCache.ccCount.addAndGet((long)c.getCount());

                            try {
                                Date CCtime = dateFormat.parse(c.getTime());
                                if (startime == null || ((Date)startime).after(CCtime)) {
                                    AttackReportCache.firstCC = entity;
                                }
                            } catch (ParseException var17) {
                                var17.printStackTrace();
                            }
                        }
                    }

                    current5Min.getList().clear();
                    previous = current;
                }
            }

        }
    }
}

